<template>
  <div>
    <el-dialog
      title="修改用户信息"
      :visible.sync="editDialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="setData" label-width="80px">
        <el-form-item label="用户名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-date-picker
            v-model="form.userDate1"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="用户地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="getChangeData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["editDialogVisible", "obj","i"],
  data() {
    return {
      form: {
        name: "",
        userDate1: "",
        phone: "",
        address: "",
      },
      dialogVisibleState: true,
      index:null
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.cancelEdit();
        })
        .catch((_) => {});
    },
    cancelEdit() {
      this.dialogVisibleState = false;
      this.$emit("changeEditVisible", this.dialogVisibleState);
    },
    getChangeData(){
        if((typeof this.form.userDate1) !== "string"){
            this.form.userDate1 = this.setDateTime();
        }
        this.index = this.i;
        let data = {
          dataForm:this.form,
          i:this.index
        }
        this.$emit('editData',data);
        this.cancelEdit();
    },
    setDateTime(){
      let DateString = this.form.userDate1.toLocaleDateString();
      let date = DateString.split('/');
      if(date[1]<10){
        date[1] = '0'+date[1]
      }
      if(date[2]<10){
        date[2] = '0'+date[2]
      }
      return date.join('-')
    }
  },
  computed:{
      setData(){
        this.form.name = this.obj.name
        this.form.userDate1 = this.obj.date
        this.form.address = this.obj.address
        this.form.phone = this.obj.userPhone
        return this.form
    },
  }
};
</script>

<style>
</style>